home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / lang / fpc09905c.lha / fpc / inc / text.inc < prev    next >
Text File  |  1998-09-21  |  29KB  |  1,263 lines

  1. {
  2.     $Id: text.inc,v 1.21 1998/08/17 22:42:17 michael Exp $
  3.     This file is part of the Free Pascal Run time library.
  4.     Copyright (c) 1993,97 by the Free Pascal development team
  5.  
  6.     See the file COPYING.FPC, included in this distribution,
  7.     for details about the copyright.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12.  
  13.  **********************************************************************}
  14.  
  15. {
  16.   Possible Defines:
  17.  
  18.   EXTENDED_EOF    Use extended EOF checking for textfile, necessary for
  19.                   Pipes and Sockets under Linux
  20.   EOF_CTRLZ       Is Ctrl-Z (#26) a EOF mark for textfiles
  21.   SHORT_LINEBREAK Use short Linebreaks #10 instead of #10#13
  22.  
  23.   Both EXTENDED_EOF and SHORT_LINEBREAK are defined in the Linux system
  24.   unit (syslinux.pp)
  25. }
  26.  
  27. {****************************************************************************
  28.                     subroutines For TextFile handling
  29. ****************************************************************************}
  30.  
  31.  
  32. Procedure FileCloseFunc(Var t:TextRec);
  33. Begin
  34.   Do_Close(t.Handle);
  35.   t.Handle:=UnusedHandle;
  36. End;
  37.  
  38.  
  39. Procedure FileReadFunc(var t:TextRec);
  40. Begin
  41.   t.BufEnd:=Do_Read(t.Handle,Longint(t.Bufptr),t.BufSize);
  42.   t.BufPos:=0;
  43. End;
  44.  
  45.  
  46. Procedure FileWriteFunc(var t:TextRec);
  47. Begin
  48.   Do_Write(t.Handle,Longint(t.Bufptr),t.BufPos);
  49.   t.BufPos:=0;
  50. End;
  51.  
  52.  
  53.  
  54. Procedure FileOpenFunc(var t:TextRec);
  55. var
  56.   Flags : Longint;
  57. Begin
  58.   Case t.mode Of
  59.     fmInput : Flags:=$1000;
  60.    fmOutput : Flags:=$1101;
  61.    fmAppend : Flags:=$1011;
  62.   else
  63.    HandleError(102);
  64.   End;
  65.   Do_Open(t,PChar(@t.Name),Flags);
  66.   t.CloseFunc:=@FileCloseFunc;
  67.   t.FlushFunc:=nil;
  68.   if t.Mode=fmInput then
  69.    t.InOutFunc:=@FileReadFunc
  70.   else
  71.    begin
  72.      t.InOutFunc:=@FileWriteFunc;
  73.    { Only install flushing if its a NOT a file }
  74.      if Do_Isdevice(t.Handle) then
  75.       t.FlushFunc:=@FileWriteFunc;
  76.    end;
  77. End;
  78.  
  79.  
  80. Procedure assign(var t:Text;const s:String);
  81. Begin
  82.   FillChar(t,SizEof(TextRec),0);
  83. { only set things that are not zero }
  84.   TextRec(t).Handle:=UnusedHandle;
  85.   TextRec(t).mode:=fmClosed;
  86.   TextRec(t).BufSize:=128;
  87.   TextRec(t).Bufptr:=@TextRec(t).Buffer;
  88.   TextRec(t).OpenFunc:=@FileOpenFunc;
  89.   Move(s[1],TextRec(t).Name,Length(s));
  90. End;
  91.  
  92.  
  93. Procedure assign(var t:Text;p:pchar);
  94. begin
  95.   Assign(t,StrPas(p));
  96. end;
  97.  
  98.  
  99. Procedure assign(var t:Text;c:char);
  100. begin
  101.   Assign(t,string(c));
  102. end;
  103.  
  104.  
  105. Procedure Close(var t : Text);[Public,Alias: 'CLOSE_TEXT',IOCheck];
  106. Begin
  107.   if InOutRes <> 0 then Exit;
  108.   If (TextRec(t).mode<>fmClosed) Then
  109.    Begin
  110.    { Write pending buffer }
  111.      If Textrec(t).Mode=fmoutput then
  112.        FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  113.      TextRec(t).mode:=fmClosed;
  114.    { Only close functions not connected to stdout.}
  115.      If ((TextRec(t).Handle<>StdInputHandle) or
  116.          (TextRec(t).Handle<>StdOutputHandle) or
  117.          (TextRec(t).Handle<>StdErrorHandle)) Then
  118.       FileFunc(TextRec(t).CloseFunc)(TextRec(t));
  119.    End;
  120. End;
  121.  
  122.  
  123. Procedure OpenText(var t : Text;mode,defHdl:Longint);
  124. Begin
  125.   Case TextRec(t).mode Of {This gives the fastest code}
  126.    fmInput,fmOutput,fmInOut : Close(t);
  127.    fmClosed : ;
  128.   else
  129.    Begin
  130.      InOutRes:=102;
  131.      exit;
  132.    End;
  133.   End;
  134.   TextRec(t).mode:=word(mode);
  135.   FileFunc(TextRec(t).OpenFunc)(TextRec(t))
  136. End;
  137.  
  138.  
  139. Procedure Rewrite(var t : Text);[IOCheck];
  140. Begin
  141.   If InOutRes <> 0 then exit;
  142.   OpenText(t,fmOutput,1);
  143. End;
  144.  
  145.  
  146. Procedure Reset(var t : Text);[IOCheck];
  147. Begin
  148.   If InOutRes <> 0 then exit;
  149.   OpenText(t,fmInput,0);
  150. End;
  151.  
  152.  
  153. Procedure Append(var t : Text);[IOCheck];
  154. Begin
  155.   If InOutRes <> 0 then exit;
  156.   OpenText(t,fmAppend,1);
  157. End;
  158.  
  159.  
  160. Procedure Flush(var t : Text);[IOCheck];
  161. Begin
  162.   If InOutRes <> 0 then exit;
  163.   If TextRec(t).mode<>fmOutput Then
  164.    exit;
  165. { Not the flushfunc but the inoutfunc should be used, becuase that
  166.   writes the data, flushfunc doesn't need to be assigned }
  167.   FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  168. End;
  169.  
  170.  
  171. Procedure Erase(var t:Text);[IOCheck];
  172. Begin
  173.   If InOutRes <> 0 then exit;
  174.   If TextRec(t).mode=fmClosed Then
  175.    Do_Erase(PChar(@TextRec(t).Name));
  176. End;
  177.  
  178.  
  179. Procedure Rename(var t : text;p:pchar);[IOCheck];
  180. Begin
  181.   If InOutRes <> 0 then exit;
  182.   If TextRec(t).mode=fmClosed Then
  183.    Begin
  184.      Do_Rename(PChar(@TextRec(t).Name),p);
  185.      Move(p^,TextRec(t).Name,StrLen(p)+1);
  186.    End;
  187. End;
  188.  
  189.  
  190. Procedure Rename(var t : Text;const s : string);[IOCheck];
  191. var
  192.   p : array[0..255] Of Char;
  193. Begin
  194.   If InOutRes <> 0 then exit;
  195.   Move(s[1],p,Length(s));
  196.   p[Length(s)]:=#0;
  197.   Rename(t,Pchar(@p));
  198. End;
  199.  
  200.  
  201. Procedure Rename(var t : Text;c : char);[IOCheck];
  202. var
  203.   p : array[0..1] Of Char;
  204. Begin
  205.   If InOutRes <> 0 then exit;
  206.   p[0]:=c;
  207.   p[1]:=#0;
  208.   Rename(t,Pchar(@p));
  209. End;
  210.  
  211.  
  212. Function Eof(Var t: Text): Boolean;[IOCheck];
  213. Begin
  214.   If InOutRes <> 0 then exit;
  215. {$IFNDEF EXTENDED_EOF}
  216.   {$IFDEF EOF_CTRLZ}
  217.     Eof:=TextRec(t).Buffer[TextRec(t).BufPos]=#26;
  218.     If Eof Then
  219.      Exit;
  220.   {$ENDIF EOL_CTRLZ}
  221.   Eof:=(Do_FileSize(TextRec(t).Handle)<=Do_FilePos(TextRec(t).Handle));
  222.   If Eof Then
  223.    Eof:=TextRec(t).BufEnd <= TextRec(t).BufPos;
  224. {$ELSE EXTENDED_EOF}
  225.   { The previous method will NOT work on stdin and pipes or sockets.
  226.     So how to do it ?
  227.      1) Check if characters in buffer - Yes ? Eof=false;
  228.      2) Read buffer full. If 0 Chars Read : Eof !
  229.     Michael.}
  230.   If TextRec(T).mode=fmClosed Then  { Sanity Check }
  231.    Begin
  232.      Eof:=True;
  233.      Exit;
  234.    End;
  235.   If (TextRec(T).BufPos < TextRec(T).BufEnd) Then
  236.    Begin
  237.      Eof:=False;
  238.      Exit
  239.    End;
  240.   TextRec(T).BufPos:=0;
  241.   TextRec(T).BufEnd:=Do_Read(TextRec(T).Handle,Longint(TextRec(T).BufPtr),TextRec(T).BufSize);
  242.   If TextRec(T).BufEnd<0 Then
  243.    TextRec(T).BufEnd:=0;
  244.   Eof:=(TextRec(T).BufEnd=0);
  245. {$ENDIF EXTENDED_EOF}
  246. End;
  247.  
  248.  
  249. Function Eof:Boolean;
  250. Begin
  251.   Eof:=Eof(Input);
  252. End;
  253.  
  254.  
  255. Function SeekEof (Var F : Text) : Boolean;
  256. Var
  257.   TR   : ^TextRec;
  258.   Temp : Longint;
  259. Begin
  260.   TR:=@TextRec(f);
  261.   If TR^.mode<>fmInput Then exit (true);
  262.   SeekEof:=True;
  263.   {No data in buffer ? Fill it }
  264.   If TR^.BufPos>=TR^.BufEnd Then
  265.    FileFunc(TR^.InOutFunc)(TR^);
  266.  
  267.   Temp:=TR^.BufPos;
  268.   while (TR^.BufPos<TR^.BufEnd) Do
  269.    Begin
  270.      If (TR^.Bufptr^[Temp] In [#9,#10,#13,' ']) Then
  271.       Inc(Temp)
  272.      else
  273.       Begin
  274.         SeekEof:=False;
  275.         TR^.BufPos:=Temp;
  276.         exit;
  277.       End;
  278.      If Temp>=TR^.BufEnd Then
  279.       Begin
  280.         FileFunc(TR^.InOutFunc)(TR^);
  281.         Temp:=TR^.BufPos+1;
  282.       End;
  283.    End;
  284. End;
  285.  
  286.  
  287. Function SeekEof : Boolean;
  288. Begin
  289.   SeekEof:=SeekEof(Input);
  290. End;
  291.  
  292.  
  293. Function Eoln(var t:Text) : Boolean;
  294. Begin
  295. { maybe we need new data }
  296.   If TextRec(t).BufPos>=TextRec(t).BufEnd Then
  297.    FileFunc(TextRec(t).InOutFunc)(TextRec(t));
  298.   Eoln:=Eof(t) or (TextRec(t).Bufptr^[TextRec(t).BufPos] In [#10,#13]);
  299. End;
  300.  
  301.  
  302. Function Eoln : Boolean;
  303. Begin
  304.   Eoln:=Eoln(Input);
  305. End;
  306.  
  307.  
  308. Function SeekEoln (Var F : Text) : Boolean;
  309. Var
  310.   TR   : ^TextRec;
  311.   Temp : Longint;
  312. Begin
  313.   TR:=@TextRec(f);
  314.   If TR^.mode<>fmInput Then
  315.    exit (true);
  316.   SeekEoln:=True;
  317.   {No data in buffer ? Fill it }
  318.   If TR^.BufPos>=TR^.BufEnd Then
  319.    FileFunc(TR^.InOutFunc)(TR^);
  320.   Temp:=TR^.BufPos;
  321.   while (TR^.BufPos<TR^.BufEnd) Do
  322.    Begin
  323.      Case (TR^.Bufptr^[Temp]) Of
  324.       #10 : Exit;
  325.    #9,' ' : Inc(Temp)
  326.      else
  327.       Begin
  328.         SeekEoln:=False;
  329.         TR^.BufPos:=Temp;
  330.         exit;
  331.       End;
  332.      End;
  333.      If Temp>=TR^.BufEnd Then
  334.       Begin
  335.         FileFunc(TR^.InOutFunc)(TR^);
  336.         Temp:=TR^.BufPos+1;
  337.       End;
  338.    End;
  339. End;
  340.  
  341.  
  342. Function SeekEoln : Boolean;
  343. Begin
  344.   SeekEoln:=SeekEoln(Input);
  345. End;
  346.  
  347.  
  348. Procedure SetTextBuf(Var F : Text; Var Buf);[INTERNPROC: In_settextbuf_file_x];
  349.  
  350.  
  351. Procedure SetTextBuf(Var F : Text; Var Buf; Size : Word);
  352. Begin
  353.   TextRec(f).BufPtr:=@Buf;
  354.   TextRec(f).BufSize:=Size;
  355.   TextRec(f).BufPos:=0;
  356.   TextRec(f).BufEnd:=0;
  357. End;
  358.  
  359.  
  360. {*****************************************************************************
  361.                                Write(Ln)
  362. *****************************************************************************}
  363.  
  364. Procedure WriteBuffer(var f:TextRec;var b;len:longint);
  365. var
  366.   p   : pchar;
  367.   left,
  368.   idx : longint;
  369. begin
  370.   p:=pchar(@b);
  371.   idx:=0;
  372.   left:=f.BufSize-f.BufPos;
  373.   while len>left do
  374.    begin